/*
 * JavaLoggingAdapter.java  0.4.0 / Oct 27, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.logging.adapters;

import java.util.logging.Level;
import java.util.logging.Logger;

import com.cute4j.Cute4j;
import com.cute4j.logging.LoggingAdapter;

/**
 * Logging Adapter for Java 1.4 Logging API.
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class JavaLoggingAdapter implements LoggingAdapter {

// Private Static Fields. ------------------------------------------------------

  private static final int LOG_METHOD_INDEX = 2;

// Private Fields. -------------------------------------------------------------

  private final Logger logger; // NOPMD - GC

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Jdk14LoggingAdapter object.
   */
  public JavaLoggingAdapter() {
    logger = Logger.getLogger(Cute4j.class.getName()); // NOPMD
  }

// LoggingAdapter Interface Implementation. ------------------------------------

  /**
   * {@inheritDoc}
   */
  public String getName() {
    return "Java Logging Adapter";
  }

  /**
   * {@inheritDoc}
   */
  public boolean isLoggable(int level) {
    return logger.isLoggable(convertLevel(level));
  }

  /**
   * {@inheritDoc}
   */
  public int getLevel() {
    return convertLevel(logger.getLevel());
  }

  /**
   * {@inheritDoc}
   */
  public void setLevel(int level) {
    logger.setLevel(convertLevel(level));
  }

  /**
   * {@inheritDoc}
   */
  public void log(int level, String message, Throwable t) {
    String className = null;
    String methodName = null;
    Throwable traceException = new Throwable(); // NOPMD - fake exception
    StackTraceElement[] stackTrace = traceException.getStackTrace();
    if ((stackTrace != null) && (stackTrace.length > LOG_METHOD_INDEX)) {
      StackTraceElement ste = stackTrace[LOG_METHOD_INDEX];
      className = ste.getClassName();
      methodName = ste.getMethodName();
    }
    logger.logp(convertLevel(level), className, methodName, message, t);
  }

  /**
   * {@inheritDoc}
   */
  public void log(int level, String fileName, int lineNumber, String message,
                  Throwable t) {
    logger.logp(convertLevel(level), fileName, String.valueOf(lineNumber),
                message, t);
  }

// Private Methods. ------------------------------------------------------------

  private Level convertLevel(int level) {
    switch (level) {
      case com.cute4j.logging.Logger.LEVEL_INFO:
        return Level.INFO;
      case com.cute4j.logging.Logger.LEVEL_WARN:
        return Level.WARNING;
      default:
        return Level.SEVERE;
    }
  }

  private int convertLevel(Level level) {
    int l = level.intValue();
    if (l <= Level.INFO.intValue()) {
      return com.cute4j.logging.Logger.LEVEL_INFO;
    } else if (l <= Level.WARNING.intValue()) {
      return com.cute4j.logging.Logger.LEVEL_WARN;
    }
    return com.cute4j.logging.Logger.LEVEL_ERROR;
  }

}
